//
// Created by snake on 2022/11/8.
//

// #ifndef AOS_ARG_H
// #define AOS_ARG_H
#pragma once

#include <string>
#include <getopt.h>
#include <ostream>

using namespace std;

struct Args {
    int port = 12345;
};

struct ServerArgs : public Args {
    string listen = "127.0.0.1";
    bool hidden = false;
    // 默认当前文件夹
    string dir = ".";
};

struct ClientArgs : public Args {
    bool list = true;
    string server = "127.0.0.1";
    string dl;
    string output;
};


/**
 * 服务器命令行参数
 */
ServerArgs parse_serv_args(int argc, char *argv[]) {
    option long_options[] = {
            {"listen", required_argument, nullptr, 'l'},
            {"hidden", no_argument,       nullptr, 'H'},
            {"port",   required_argument, nullptr, 'p'},
    };

    ServerArgs args;
    int opt, opt_idx;
    while ((opt = getopt_long(argc,
                              argv,
                              "l:Hp:",
                              long_options,
                              &opt_idx)) != -1) {
        switch (opt) {
            case 'l':
                args.listen = optarg;
                break;
            case 'H':
                args.hidden = true;
                break;
            case 'p':
                args.port = strtol(optarg, nullptr, 10);
                break;
            default:
                break;
        }
    }

    if (optind < argc) {
        args.dir = argv[optind];
    }

    return args;
}

ClientArgs parse_cli_args(int argc, char *argv[]) {
    option long_options[] = {
            {"list",   no_argument,       nullptr, 'l'},
            {"server", required_argument, nullptr, 's'},
            {"port",   required_argument, nullptr, 'p'},
            {"dl",     required_argument, nullptr, 'd'},
            {"output", required_argument, nullptr, 'o'},
    };

    ClientArgs args;
    int opt, opt_idx;
    while ((opt = getopt_long(argc,
                              argv,
                              "ls:p:d:o:",
                              long_options,
                              &opt_idx)) != -1) {
        switch (opt) {
            case 's':
                args.server = optarg;
                break;
            case 'p':
                args.port = strtol(optarg, nullptr, 10);
                break;
            case 'd':
                args.list = false;
                args.dl = optarg;
                break;
            case 'o':
                args.output = optarg;
                break;
                // case 'l':
            default:
                break;
        }
    }

    return args;
}


// #endif //AOS_ARG_H
